package com.xcity.game.player;

import java.text.MessageFormat;
import java.util.List;

import com.xcity.db.entity.Player;
import com.xcity.game.common.Cause;
import com.xcity.game.common.Currency;
import com.xcity.game.common.MessageConst;
import com.xcity.game.employee.EmployeeService;
import com.xcity.game.event.GameEvents;
import com.xcity.game.player.template.DestinyTemplate;
import com.xcity.pomelo.PomeloRoute;
import com.xcity.pomelo.request.DestinyInfoRequest;
import com.xcity.pomelo.request.DestinyLevelUpRequest;
import com.xcity.pomelo.response.DestinyInfoResponse;
import com.xcity.pomelo.response.DestinyLevelUpResponse;
import com.xcity.pomelo.response.HintResponse;

import naga.x.App;
import naga.x.game.template.TemplateService;
import naga.x.net.Session;
import naga.x.net.annotation.OPHandler;
import naga.x.net.impl.pomelo.annotation.PomeloOP;
import naga.x.service.Service;
import naga.x.service.ServiceException;

/**
 * 角色养成（天命）
 * @author yang.li
 * @note 玩家升品与员工升品相同{@link EmployeeService#requestQualityLevelUpInfo} {@link EmployeeService#requestQualityLevelUp}
 *
 */
@OPHandler
public class PlayerTrainService implements Service {
	
	public static int QUALITY_LEVEL_UP_TEMPLATE_ID = -1; // 玩家升品表id @see EmployeeQualityUpTemplate
	
	protected List<DestinyTemplate> destinyTemplates; // 天命模板

	@Override
	public void startup() throws ServiceException {
		destinyTemplates = App.getApp().getServiceManager().get(TemplateService.class).getTemplatesList(DestinyTemplate.class);
	}

	@Override
	public void shutdown() throws ServiceException {
	}

	@Override
	public String getId() {
		return getClass().getName();
	}
	
	public DestinyTemplate getDestinyTemplate(int level) {
		int index = level - 1;
		return index >= 0 && index < destinyTemplates.size() ? destinyTemplates.get(index) : null;
	}
	
	public int getStarLvl(Player player) {
		DestinyTemplate dt = getDestinyTemplate(player.getDestinyLvl());
		return dt != null ? dt.getStarLvl() : 0;
	}
	
	public int getMaxDestinyLevel() {
		return destinyTemplates.size();
	}
	
	public List<DestinyTemplate> getDestinyTemplates() {
		return destinyTemplates;
	}
	
	@PomeloOP(PomeloRoute.DESTINY_INFO)
	protected void requestDestinyInfo(Session session, DestinyInfoRequest req) {
		Player player = session.getClient();
		session.send(new DestinyInfoResponse(req.getSerial(), player, this));
	}
	
	@PomeloOP(PomeloRoute.DESTINY_LEVEL_UP)
	protected void requestDestinyLevelUp(Session session, DestinyLevelUpRequest req) {
//		DestinyLevelUpResponse
		Player player = session.getClient();
		if (player.getDestinyLvl() >= getMaxDestinyLevel()) {
			session.send(new HintResponse(req.getSerial(), MessageConst.PLAYER_DESTINY_LEVEL_MAXED));
			return;
		}
		DestinyTemplate next = getDestinyTemplate(player.getDestinyLvl() + 1);
		if (next == null) { // impossible
			session.send(new HintResponse(req.getSerial(), MessageConst.PLAYER_DESTINY_LEVEL_MAXED));
			return;
		}
		if (player.getLevel() < next.getLevelLimit()) {
			session.send(new HintResponse(req.getSerial(), MessageFormat.format(MessageConst.PLAYER_DESTINY_LEVEL_UP_LIMITED, next.getLevelLimit())));
			return;
		}
		if (next.getGold() > 0 && !player.isEnough(Currency.GOLD, next.getGold())) {
			session.send(new HintResponse(req.getSerial(), MessageConst.GOLD_NOT_ENOUGH));
			return;
		}
		if (!player.decStageStars(next.getCostStars(), Cause.DESTINY_LEVEL_UP)) {
			session.send(new HintResponse(req.getSerial(), MessageConst.STAGE_STARS_NOT_ENOUGH));
			return;
		}
		player.costGold(next.getGold(), Cause.DESTINY_LEVEL_UP);
		int oldStarLvl = getStarLvl(player);
		player.addDestinyLevel(1, next, Cause.MANUAL);
		if (next.getStarLvl() > oldStarLvl) {
			App.getApp().getEventManager().addEvent(GameEvents.EVENT_PLAYER_STAR_LVL_UP, player, oldStarLvl, next.getStarLvl());
		}
		session.send(new DestinyLevelUpResponse(req.getSerial(), player, next, this));
	}

}
